package com.filebackup.daemon;

import java.util.Iterator;
import java.util.concurrent.PriorityBlockingQueue;

import com.filebackup.cloudapi.ICloudApi;

public class DownloadWorker extends DaemonWorker {

    protected PriorityBlockingQueue<DownloadJob> joblist = new PriorityBlockingQueue<DownloadJob>();

    public DownloadWorker(int id, String name, ICloudApi api) {
        super(id, name, api);

    }

    @Override
    protected void init() {

    }

    @Override
    public void run() {

        while (true) {
            DownloadJob job;
            try {
                job = joblist.take();
                api.downloadFile(getApiPath(), job.cloudFile, job.saveFile);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    private String getApiPath() {
        return api.getCloudPath();
    }

    public void assignJob(DownloadJob job) {
        joblist.put(job);
    }

    public long getListJobSize() {

        long size = 0;
        Iterator<DownloadJob> itr = joblist.iterator();
        while (itr.hasNext()) {
            DownloadJob job = itr.next();
            size += job.getJobSize();
        }

        return size;
    }

}
